import sys, time, random, math, pygame
from pygame.locals import *

class NSprite(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.master_image = None
        self.frame = 0
        self.frame_width = 1
        self.frame_height = 1
        self.old_frame = -1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0
        self.direction = 0
        self.velocity = Point(0.0, 0.0)
        self.moving = False
        self.move_speed = 0
        self.bomb = False

    # X property
    def _getx(self):
        return self.rect.x

    def _setx(self, value):
        self.rect.x = value

    X = property(_getx, _setx)

    # Y property
    def _gety(self):
        return self.rect.y

    def _sety(self, value):
        self.rect.y = value

    Y = property(_gety, _sety)

    # position property
    def _getpos(self):
        return self.rect.topleft

    def _setpos(self, pos):
        self.rect.topleft = pos

    position = property(_getpos, _setpos)

    # move_speed property
    def _get_move_speed(self):
        return self.move_speed

    def _set_move_speed(self, value):
        self.move_speed = value

    mspeed = property(_get_move_speed, _set_move_speed)

    def load(self, filename, width, height, rows, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.rect = Rect(0, 0, width, height)
        self.rows = rows
        self.columns = columns
        # try to auto-calculate total frames
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width // width) * (rect.height // height) - 1

    def update(self, current_time, rate=30):
        out = self.UpdatePos(1024, 768)
        if out:
            self.kill()
            return
        # update animation frame number
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        # build current frame only if it changed
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width
            frame_y = self.direction * self.frame_height
            if self.direction >= self.rows:
                frame_y = (self.rows - 1) * self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame

    def SetMoveDir(self, dir):
        self.moving = True
        self.direction = dir
        self.first_frame = self.direction * self.columns
        self.last_frame = self.first_frame + self.columns - 1
        if self.frame < self.first_frame:
            self.frame = self.first_frame
        self.velocity = self.CalcVelocity()

    def CalcVelocity(self):
        velocity = Point(0, 0)
        delta = self.move_speed
        if self.direction == 0:  # 上
            velocity.y = -delta
        elif self.direction == 2:  # 右
            velocity.x = delta
        elif self.direction == 4:  # 下
            velocity.y = delta
        elif self.direction == 6:  # 左
            velocity.x = -delta
        return velocity

    def Stop(self):
        self.moving = False
        self.frame = self.first_frame = self.last_frame

    def UpdatePos(self, width, height):
        if not self.moving:
            return False
        self.rect.centerx += self.velocity.x
        self.rect.centery += self.velocity.y
        if self.rect.centerx < 0 or self.rect.centerx > width:
            return True
        if self.rect.centery < 0 or self.rect.centery > height:
            return True
        return False

    def __str__(self):
        return str(self.frame) + "," + str(self.first_frame) + \
               "," + str(self.last_frame) + "," + str(self.frame_width) + \
               "," + str(self.frame_height) + "," + str(self.columns) + \
               "," + str(self.rect)

class Point(object):
    def __init__(self, x, y):
        self.__x = x
        self.__y = y

    #X property
    def getx(self): return self.__x
    def setx(self, x): self.__x = x
    x = property(getx, setx)

    #Y property
    def gety(self): return self.__y
    def sety(self, y): self.__y = y
    y = property(gety, sety)

    def __str__(self):
        return "{X:" + "{:.0f}".format(self.__x) + \
            ",Y:" + "{:.0f}".format(self.__y) + "}"